<?php
/*
*	created by cbq in 2017.02.08
*	站点相关模型
*/
defined('BASEPATH') OR exit('No direct script access allowed');
class Info_m extends MY_Model {
    public function __construct()
    {
        parent::__construct();
        $this->table='info';
    }
    public function sel($where=array(),$field='*'){
    	$this->com_where($where);
    	$this->db->select($field);
      	$query=$this->db->get($this->table);
     	$data=$query->row_array();
      	return $data;
    }
    //文章ajax 分页
    public function ajaxall($where=array(),$field="*",$order='A.addtime desc'){
        $pagesize=getConfig('pagesize');
        $this->com_where($where);
        $this->db->select('count(A.infoid) as counts');
        $this->db->from($this->table." as A");
        $this->db->join('sites as B','B.id=A.siteid');
        $this->db->join('column as C','C.colid=A.colid');
        $this->db->join('adminuser as D','D.id=A.userid');
        $query=$this->db->get();
        $counts=$query->row_array();

        $page=isset($_REQUEST['page'])?$_REQUEST['page']:1;
        $startRow=$pagesize * ($page - 1);
        $this->com_where($where);
        $this->db->select($field);
        $this->db->from($this->table." as A");
        $this->db->join('sites as B','B.id=A.siteid');
        $this->db->join('column as C','C.colid=A.colid');
        $this->db->join('adminuser as D','D.id=A.userid');
        $this->db->order_by($order);
        $this->db->limit($pagesize,$startRow);
        $query2=$this->db->get();
        $data['list']=$query2->result_array();
        $data['pageshow']=$this->pageajax_show($pagesize,$counts['counts']);
        return $data;
    }
    //添加文章
    public function add($data){
            $addtime=time();
            $data['addtime']=$addtime;
            $infoarr=$this->return_add($data);
            $this->db->trans_begin();
            $this->db->insert($this->table,$infoarr);
            //获取文章id
            $infoid=$this->db->insert_id('infoid');
            if(!$infoid){$this->db->trans_rollback();return false;}
            $data['infoid']=$infoid;
   
            //插入所属专题
            $special_arr=$this->return_special_add($data);
            if(!empty($special_arr)){
                $this->db->insert('special_r',$special_arr);
                if(!$this->db->affected_rows()){
                    $this->db->trans_rollback();return false;
                }
            }
            //写入栏目文章数
            $CI = get_instance();
            $CI->load->model('column_m');
            if($CI->column_m->cloumn_infonum($data['colid'])===false){
                 $this->db->trans_rollback();return false;
            }
            $this->db->trans_commit();
            return $infoid;
    }
    //编辑推荐 置顶 审核等
    public function edit($where,$data){
        $this->com_where($where);
        $this->db->update($this->table,$data);
        return $this->db->affected_rows();
    }
    //编辑文章时数据处理
    public function return_edit($data){
        $arr=array();
        $arr['infoid']=$data['infoid'];
        if(isset($data['title'])){$arr['title']=str_safe($data['title']);}
        if(isset($data['defaultpicurl'])){$arr['defaultpicurl']=$data['defaultpicurl'];}
        if(isset($data['intro'])){$arr['intro']=str_safe($data['intro']);}
        if(isset($data['mediafile'])){$arr['mediafile']=str_safe($data['mediafile']);}
        if(isset($data['copyfrom'])){$arr['copyfrom']=$data['copyfrom'];}
        if(isset($data['recommendflag'])){$arr['recommendflag']=$data['recommendflag'];}
        if(isset($data['headflag'])){$arr['titilecolor']=$data['headflag'];}
        if(isset($data['titilecolor'])){ $arr['titilecolor']=$data['titilecolor'];}
        $arr['verify']=isset($data['verify'])?$data['verify']:1;
        if(isset($data['verifyuser'])){$arr['verifyuser']=$data['verifyuser'];}
        if(isset($data['addtime'])){$arr['addtime']=$data['addtime'];}
        if(isset($data['finalverify'])){$arr['finalverify']=$data['finalverify'];}
        if(isset($data['hits'])){$arr['hits']=$data['hits'];}
        if(isset($data['content'])){$arr['content']=str_safe($data['content']);}
        return $arr;
    }
    //添加文章时数据处理
    private function return_add($data){
        $addtime=time();
        $data['addtime']=$addtime;
        $eduadmininfo=$_SESSION['eduadmininfo'];
        $eduadmininfo=unserialize($eduadmininfo);
        $arr=array();
        $arr['colid']=$data['colid'];
        $arr['siteid']=$data['sitesid'];
        $arr['title']=str_safe($data['title']);
        $arr['titilecolor']=$data['titilecolor'];
        $arr['defaultpicurl']=$data['defaultpicurl'];
        $arr['intro']=str_safe($data['intro']);
        $arr['mediafile']=str_safe($data['mediafile']);
        $arr['copyfrom']=$data['copyfrom'];
        $arr['recommendflag']=$data['recommendflag'];
        $arr['headflag']=$data['headflag'];
        $arr['userid']=$eduadmininfo['id'];
        $arr['addtime']=$addtime;
        $arr['verify']=isset($data['verify'])?$data['verify']:1;
        $arr['verifyuser']=$data['verifyuser'];
        $arr['finalverify']=$data['finalverify'];
        $arr['hits']=isset($data['hits'])?$data['hits']:0;
        $arr['seq']=$addtime;
        $arr['delflag']=0;
        $arr['content']=str_safe($data['content']);
        return $arr;
    }
    
    //添加文章时所属专题添
    private function return_special_add($data){
        $arr=array();
        if($data['specialid']){
            $arr=array();
            $arr['ctypeid']=$data['specialtypeid'];
            $arr['specialid']=$data['specialid'];
            $arr['infoid']=$data['infoid'];
        } 
        return $arr;
    }
 }